package com.jy.appmarket.newnetwork.api;

import java.io.File;

import com.jy.appmarket.AppMarketApplication;
import com.jy.appmarket.newnetwork.Cache;
import com.jy.appmarket.newnetwork.Network;
import com.jy.appmarket.newnetwork.RequestQueue;
import com.jy.appmarket.newnetwork.toolbox.BasicNetwork;
import com.jy.appmarket.newnetwork.toolbox.DiskBasedCache;
import com.jy.appmarket.newnetwork.toolbox.HurlStack;
import com.jy.appmarket.utils.LocationUtil;
import com.jy.appmarket.utils.StorageConfig;

import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Point;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.WindowManager;

public final class MarketApi {
	private static final String TAG = MarketApi.class.getName();

	private static File cacheDir = new File(StorageConfig.CACHE_DIR);
	private static Cache cache = new DiskBasedCache(cacheDir);
	private static Network network = new BasicNetwork(new HurlStack());
	private static Context sContext;

	public static RequestQueue newApiRequestQueue(Context context) {
		if (sContext == null && context != null) {
			sContext = context.getApplicationContext();
		}
		RequestQueue queue = new RequestQueue(cache, network);
		queue.start();
		return queue;
	}

	////////////////////////////////////////////////////////////////////////////////////////////////
	public static String currentNetworkType() {
		if (sContext == null) {
			return "";
		}

		ConnectivityManager cm = (ConnectivityManager) sContext.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo ni = cm.getActiveNetworkInfo();

		if (ni == null) {
			return "";
		}
		switch (ni.getType()) {
		case ConnectivityManager.TYPE_WIFI:
			return "wifi";
		case ConnectivityManager.TYPE_MOBILE:
			return "mobile";
		}
		return "";
	}

	public static String getScreenSize() {
		if (sContext == null) {
			return null;
		}

		try {
			WindowManager wm = (WindowManager) sContext.getSystemService(Context.WINDOW_SERVICE);
			Point pt = new Point();
			wm.getDefaultDisplay().getSize(pt);
			return pt.x + "*" + pt.y;
		} catch (Exception e) {
			// ignore todo
		}
		return null;
	}

	public static String[] getLocation() {
		if (sContext == null) {
			return null;
		}
		LocationUtil lu = LocationUtil.getInstance(sContext);
		String city = lu.getCity();
		String longitude = lu.getLongitude();
		String latitude = lu.getLatitude();

		if (TextUtils.isEmpty(city) || (TextUtils.isEmpty(longitude) && TextUtils.isEmpty(latitude))) {
			return null;
		}
		return new String[] { city, longitude, latitude };
	}

	//////////////////////////////////////////////////////////////////
	// 缓存一下，避免每次都计算
	private static String sMagic = null;
	private static String sDeviceId = null;

	public synchronized static String getMagic() {
		if (sContext == null) {
			return null;
		}
		String devId = null;
		try {
			TelephonyManager tm = (TelephonyManager) sContext.getSystemService(Context.TELEPHONY_SERVICE);
			devId = tm.getDeviceId();
		} catch (Exception e) {
		}

		if (devId != null && devId.equals(sDeviceId)) {
			return sMagic;
		}

		String aId = Settings.Secure.getString(sContext.getContentResolver(), Settings.Secure.ANDROID_ID);
		String magic = CryptoUtil.md5(aId + devId + "qwer");
		sMagic = magic;
		return magic;
	}

	static String getMyPackageName() {
		if (sContext == null) {
			return null;
		}
		return sContext.getPackageName();
	}

	static PackageInfo getMyPackageInfo() {
		if (sContext == null) {
			return null;
		}
		try {
			String packName = sContext.getPackageName();
			return sContext.getPackageManager().getPackageInfo(packName, 0);
		} catch (Exception e) {
		}
		return null;
	}

	static String[] getTelephoneInfo() {
		if (sContext == null) {
			return null;
		}
		TelephonyManager tm = (TelephonyManager) sContext.getSystemService(Context.TELEPHONY_SERVICE);
		if (tm == null) {
			return null;
		}
		return new String[] { tm.getDeviceId(), tm.getSimOperator(), tm.getSubscriberId() };
	}

	static String getMetaString(String key) {
		if (sContext == null) {
			return null;
		}
		try {
			PackageManager pm = sContext.getPackageManager();
			String pkgName = sContext.getPackageName();
			ApplicationInfo ai = pm.getApplicationInfo(pkgName, PackageManager.GET_META_DATA);
			Bundle bundle = ai.metaData;
			String channelNumber = bundle.getString(key);
			return channelNumber;
		} catch (Exception e) {
			Log.w(TAG, "failed to meta ", e);
		}
		return "unknown";
	}

	static String getAndroidId() {
		if (sContext == null) {
			return null;
		}
		try {
			ContentResolver cr = sContext.getContentResolver();
			if (cr == null) {
				return null;
			}
			return Settings.Secure.getString(cr, Settings.Secure.ANDROID_ID);
		} catch (Exception e) {
		}
		return null;
	}

	static String getUserId() {
		if (AppMarketApplication.isLogin()) {
			return AppMarketApplication.getLogin().getUserId();
		}
		return null;
	}
}
